<script setup lang="ts">
import { ref } from 'vue'

interface ButtonOption {
  text: string
  type?: 'primary' | 'default'
  onClick?: () => void
}

interface Props {
  title?: string
  buttons?: ButtonOption[]
}

withDefaults(defineProps<Props>(), {
  title: '',
  buttons: () => [],
})

const visible = ref(false)

function open() {
  visible.value = true
}

function close() {
  visible.value = false
}

// 暴露方法给父组件
defineExpose({
  open,
  close,
})
</script>

<template>
  <view v-if="visible" class="dialog-mask">
    <view class="dialog-container">
      <!-- 标题区域 -->
      <view class="dialog-header">
        <text class="dialog-title">
          <!-- {{ title }} -->
        </text>
        <image src="@/static/images/logo.png" class="dialog-header-logo" />
      </view>

      <!-- 内容区域 -->
      <view class="dialog-content">
        <view class="dialog-content-title">
          <slot name="content-title" />
        </view>
        <view class="dialog-content-sub-title">
          <slot name="content-sub-title" />
        </view>
        <slot />
      </view>

      <!-- 按钮区域 -->
      <view v-if="buttons.length > 0" class="dialog-footer">
        <view
          v-for="(btn, index) in buttons" :key="index" class="dialog-button"
          :class="[`dialog-button--${btn.type || 'default'}`]" @tap="btn.onClick?.() ?? close()"
        >
          {{ btn.text }}
        </view>
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
.dialog-content {
    border-top-left-radius: 0;
    border-top-right-radius: 0;

    &-title {
        font-size: 34rpx;
        text-align: center;
        margin-bottom: 30rpx;
    }

    &-sub-title {
        font-size: 26rpx;
        text-align: center;
        color: #999;
    }
}

.dialog {
    &-mask {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: rgba(0, 0, 0, 0.6);
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 999;
    }

    &-container {
        width: 600rpx;
        border-radius: 24rpx;
        // overflow: hidden;
        margin-top: -10vh;
    }

    &-header {
        position: relative;
        padding: 30rpx;
        text-align: center;
        border-bottom: 2rpx solid #eee;
        background: url('@/static/images/dialog/bg.png') no-repeat top center;
        background-size: cover;
        height: 60rpx;
        position: relative;

        &-logo {
            display: block;
            width: 160rpx;
            height: 160rpx;
            position: absolute;
            top: -80rpx;
            left: 50%;
            transform: translateX(-50%);
        }
    }

    &-title {
        font-size: 32rpx;
        font-weight: 500;
        color: #333;
    }

    &-close {
        position: absolute;
        right: 30rpx;
        top: 50%;
        transform: translateY(-50%);
        font-size: 40rpx;
        color: #999;
        line-height: 1;
    }

    &-content {
        padding: 50rpx 30rpx;
        min-height: 100rpx;
        background-color: #fff;
    }

    &-footer {
        display: flex;
        border-top: 2rpx solid #eee;
        background-color: #fff;
        border-bottom-right-radius: 30rpx;
        border-bottom-left-radius: 30rpx;
    }

    &-button {
        flex: 1;
        height: 88rpx;
        line-height: 88rpx;
        text-align: center;
        font-size: 32rpx;
        color: #999;
        cursor: pointer;

        &:not(:last-child) {
            border-right: 2rpx solid #eee;
        }

        &--primary {
            color: $uni-color-primary;
        }
    }
}
</style>
